package com.typhoon.spring_shiro.utils.redis;

import com.typhoon.spring_shiro.constants.PermissionConstants;
import com.typhoon.spring_shiro.utils.asserts.AssertUtils;

/**
 * redis key工具类
 * @author Typhoon
 *
 */
public final  class RedisKeyUtils {
	
	/**
	 * 分隔符
	 */
	private static final String DELIMITER = ":";
	
	private static final String WORKERID = "workerId";
	
	/**
	 * 生成key
	 * @param model
	 * @param wokerId
	 * @param objects
	 * @return
	 */
	public static String generateKey(Model model,Long workerId,Object ...objects ) {
		AssertUtils.notNull(model);
		AssertUtils.notNull(workerId);
		StringBuilder keyBuilder = new StringBuilder();
		keyBuilder.append(model.getKey());
		keyBuilder.append(DELIMITER);
		keyBuilder.append(WORKERID).append(workerId);
		if(null != objects && 0 < objects.length) {
			for(int i =0,length = objects.length; i < length; i ++) {
				keyBuilder.append(DELIMITER);
				keyBuilder.append(objects[i].toString());
			}
		}
		
		return keyBuilder.toString();
	}
	
	/**
	 * 生成key对应的模块
	 * @author Typhoon
	 *
	 */
	public enum Model {
		
		PERMISSION("permission:cache","权限模块");
		
		private String key;
		private String name;
		
		private Model(String key,String name) {
			this.key = key;
			this.name = name;
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
	
	public static void main(String[] args) {
		//System.out.println(generateKey(Model.PERMISSION, 2015824L, "role"));
		Long workerId = 2015824L;
//		String key = RedisKeyUtils.generateKey(Model.PERMISSION, workerId, PermissionConstants.ROLE);
//		System.out.println(key);
//		System.out.println(RedisKeyUtils.generateKey(Model.PERMISSION, workerId, PermissionConstants.ROLE));
//		Long appId = 2l;
//		key = RedisKeyUtils.generateKey(Model.PERMISSION, workerId, PermissionConstants.APP_PRE+appId,PermissionConstants.MENU_PRE);
//		System.out.println(key);
	}

}
